﻿package rd.hardcore.engine 
{
	import caurina.transitions.Tweener;
	import flash.desktop.Clipboard;
	import flash.desktop.ClipboardFormats;
	import flash.desktop.NativeDragActions;
	import flash.desktop.NativeDragManager;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.NativeDragEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	/**
	 * Gestion du drag and drop
	 * @author Thibault Perret
	 */
	public class DragDropManager extends Sprite
	{
		private var sp:Sprite;
		
		private const URL_PATH_MP3 : String = "file:///" + File.applicationDirectory.resolvePath("medias/sound/").nativePath;
		private var droped:int;
		private var cpt:int;
		
		private var songNames:Array = [];
		
		
		public function DragDropManager() 
		{
			//mouseEnabled = false;
			//mouseChildren = false;
			trace(URL_PATH_MP3);
			sp = new Sprite();
			sp.graphics.beginFill(0x000000, 1);
			sp.graphics.drawRect(0, 0, 200, 200);
			sp.graphics.endFill();
			sp.alpha = 0;
			addChild(sp);
			
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(NativeDragEvent.NATIVE_DRAG_OVER, dragOver);
			addEventListener(NativeDragEvent.NATIVE_DRAG_COMPLETE, dragComplete);
			addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, dragDrop);
			addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, dragEnter);
			addEventListener(NativeDragEvent.NATIVE_DRAG_EXIT, dragExit);
			addEventListener(NativeDragEvent.NATIVE_DRAG_START, dragStart);
			addEventListener(NativeDragEvent.NATIVE_DRAG_UPDATE, dragUpdate);
		}
		
		private function dragUpdate(e:NativeDragEvent):void 
		{
			trace("2: dragUpdate");
		}
		
		private function dragStart(e:NativeDragEvent):void 
		{
			trace("2: dragStart");
		}
		
		private function dragExit(e:NativeDragEvent):void 
		{
			trace("2: dragExit");
			Tweener.addTween(sp, {alpha:0,time:.5 } );
		}
		
		private function dragEnter(e:NativeDragEvent):void 
		{
			trace("2: dragEnter");
			Tweener.addTween(sp, { alpha:.2, time:.5 } );
			NativeDragManager.acceptDragDrop(this);
		}
		
		private function dragDrop(e:NativeDragEvent):void 
		{
			trace("2: dragDrop");
			NativeDragManager.dropAction = NativeDragActions.COPY;
			
			var clipBoard:Clipboard = Clipboard(e.clipboard);
			//var dropFiles:File = clipBoard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
			var dropFiles:Array = clipBoard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
			cpt = 0;
			droped = 0;
			// Gestion de plusieurs fichiers
			for each(var file:File in dropFiles) {
				switch(file.extension)
				{
					case "mp3":
						var sourceFile:File = new File();
						sourceFile = sourceFile.resolvePath(file.nativePath);
						
						//check les infos avant de copier
						/*var stream:FileStream = new FileStream();
						stream.open(file, FileMode.READ);
						*/
						songNames.push(file.name);
						
						var destination:File = new File(URL_PATH_MP3);
						destination = destination.resolvePath(file.name);
						trace("2: " + destination.name);
						trace("2: " + destination.nativePath);
						
						try {
							sourceFile.addEventListener(Event.COMPLETE, copyComplete);
							sourceFile.addEventListener(IOErrorEvent.IO_ERROR, handleError);
							sourceFile.copyToAsync(destination, true);
						}
						catch (error:Error)
						{
							trace("3: Error de copy de fichier");
						}
						
					break;
					default:
						/*	F A I L */
						dispatchEvent(new DragDropEvent(DragDropEvent.DRAGDROP_FAIL));
					break;
				}
				cpt++;
			}
		}
		
		private function handleError(e:IOErrorEvent):void 
		{
			trace("3: Error de copyToAsync "+e.text);
		}
		
		private function copyComplete(e:Event):void 
		{
			trace("2: copyComplete");
			droped++;
			trace("compteur : "+cpt + ":" + droped);
			if (droped == cpt) {
				var save:SaveData = new SaveData(songNames);
				dispatchEvent(new DragDropEvent(DragDropEvent.DRAGDROP_SUCCEED));
			}
		}
		
		private function dragComplete(e:NativeDragEvent):void 
		{
			trace("2: dragComplete");
		}
		
		private function dragOver(e:NativeDragEvent):void 
		{
			//trace("2: dragOver");
			
		}
		
		
		
	}

}
